import '../../util/_base_export.dart';
import '../../util/_view_export.dart';

import '../../blocs/code.dart';
import '../../util/app_util.dart';

class AccountWholesaleApplyPage extends StatefulWidget {
  // final String scenario;
  const AccountWholesaleApplyPage(
      // this.scenario,
      {
    Key? key,
  }) : super(key: key);
  @override
  _AccountWholesaleApplyPageState createState() => _AccountWholesaleApplyPageState();
}

class _AccountWholesaleApplyPageState extends State<AccountWholesaleApplyPage> {
  // _loadVoucher() async {
  //   AppSession.appUtil.loadVouchers({});
  // }

  GlobalKey<FormBuilderState> _fbKey_wholesale = GlobalKey<FormBuilderState>();

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance?.addPostFrameCallback((callback) {
      // _loadVoucher();
    });
  }

  List<Widget> list = List<Widget>.empty(growable :true);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        brightness: Brightness.light,
        leading: GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: () {
            Navigator.pop(context);
          },
          child: Container(
            child: Icon(
              Icons.chevron_left,
              color: AppColor.blackMain,
              size: 32,
            ),
          ),
        ),
        title: Text(
          AppText.text['account_group_apply'],
          style: TextStyle(color: AppColor.blackMain),
        ),
      ),
      body:
          // SingleChildScrollView(
          //   child:
          Container(
        color: Colors.grey[50],
        margin: EdgeInsets.only(top: 20.h),
        child: _bodyBlock(context),
      ),
      // ),
    );
  }

  Widget _bodyBlock(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 20.w),
      child: FormBuilder(
        key: _fbKey_wholesale,
        autovalidateMode: AutovalidateMode.always,
        initialValue: {
          'contact_person': '',
          'contact_phone': '',
          'company_name': '',
          'company_addr': '',
          'notes': '',
        },
        enabled: false,
        child: Column(
          // mainAxisAlignment: MainAxisAlignment.spaceBetween,
          // mainAxisSize: MainAxisSize.max,
          children: <Widget>[
            // Expanded(
            //   child:
            // Column(
            //   children: <Widget>[
            Container(
              // decoration: AppDecoration.buttonTwo, //
              margin: EdgeInsets.symmetric(horizontal: 35.w),
              padding: EdgeInsets.only(
                left: 10.w,
              ),
              child: FormBuilderTextField(
                name: "contact_person",
                decoration: InputDecoration(
                  // border: AppInput.loginInputBorder,
                  labelText: "联系人",
                ),
                // onChanged: _onChanged,
                // valueTransformer: (text) => num.tryParse(text),
                validator: (value){
                  if((value??'').isEmpty){
                    return '联系人不能为空';
                  }
                  return null;
                },
                keyboardType: TextInputType.text,
              ),
            ),
            Container(
              // decoration: AppDecoration.buttonTwo, //
              margin: EdgeInsets.symmetric(horizontal: 35.w),
              padding: EdgeInsets.only(
                left: 10.w,
              ),
              child: FormBuilderTextField(
                name: "contact_phone",
                decoration: InputDecoration(
                  // border: AppInput.loginInputBorder,
                  labelText: "电话",
                ),
                // onChanged: _onChanged,
                // valueTransformer: (text) => num.tryParse(text),
                validator: (value){
                  if((value??'').isEmpty){
                    return '电话不能为空';
                  }else if(value!.length<9||value.length>11){
                    return '联系电话错误';
                  }else{
                    return null;
                  }
                },
                keyboardType: TextInputType.phone,
              ),
            ),
            Container(
              // decoration: AppDecoration.buttonTwo, //
              margin: EdgeInsets.symmetric(horizontal: 35.w),
              padding: EdgeInsets.only(
                left: 10.w,
              ),
              child: FormBuilderTextField(
                name: "company_name",
                decoration: InputDecoration(
                  // border: AppInput.loginInputBorder,
                  labelText: "公司名字",
                ),
                // onChanged: _onChanged,
                // valueTransformer: (text) => num.tryParse(text),
                //validator: [],
                keyboardType: TextInputType.text,
              ),
            ),
            Container(
              // decoration: AppDecoration.buttonTwo, //
              margin: EdgeInsets.symmetric(horizontal: 35.w),
              padding: EdgeInsets.only(
                left: 10.w,
              ),
              child: FormBuilderTextField(
                name: "company_addr",
                decoration: InputDecoration(
                  // border: AppInput.loginInputBorder,
                  labelText: "公司地址",
                ),
                // onChanged: _onChanged,
                // valueTransformer: (text) => num.tryParse(text),
                //validator: [],
                keyboardType: TextInputType.text,
              ),
            ),
            Container(
              // decoration: AppDecoration.buttonTwo, //
              margin: EdgeInsets.symmetric(horizontal: 35.w),
              padding: EdgeInsets.only(
                left: 10.w,
              ),
              child: FormBuilderTextField(
                name: "notes",
                maxLines: 5,
                minLines: 3,
                decoration: InputDecoration(
                  // border: AppInput.loginInputBorder,
                  labelText: "备注",
                ),
                // onChanged: _onChanged,
                // valueTransformer: (text) => num.tryParse(text),
                //validator: [],
                keyboardType: TextInputType.text,
              ),
            ),
            //   ],
            // ),
            // Spacer(),
            // ),
            Expanded(
              child: Align(
                alignment: FractionalOffset.bottomCenter,
                child: Padding(
                  padding: EdgeInsets.symmetric(vertical: 20.h),
                  child: AppButton.groupApply('提交申请', () async {
                    FocusScope.of(context).requestFocus(FocusNode());
                    Map<String, dynamic> lm_addr_param = {};

                    _fbKey_wholesale.currentState?.save();
                    if (_fbKey_wholesale.currentState!.validate()) {
                      lm_addr_param['contact_person'] =
                          _fbKey_wholesale.currentState?.value['contact_person'] ?? '';
                      lm_addr_param['contact_phone'] =
                          _fbKey_wholesale.currentState?.value['contact_phone'] ?? '';
                      lm_addr_param['company_name'] =
                          _fbKey_wholesale.currentState?.value['company_name'] ?? '';
                      lm_addr_param['company_addr'] =
                          _fbKey_wholesale.currentState?.value['company_addr'] ?? '';
                      lm_addr_param['notes'] = _fbKey_wholesale.currentState?.value['notes'] ?? '';

                      var lm_result = await AppSession.appUtil.wholesaleApply(lm_addr_param);

                      if ((lm_result ?? {}).isNotEmpty) {
                        AppPopup.popupToast('S', '成功提交，请等待审核');
                        Navigator.pop(context);
                      }
                    }
                  }),
                ),
              ),
            ),
            // ),
            // ),
          ],
        ),
      ),
    );
  }
}
